using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProvider;
namespace DalatCooking.Core
{
    class ProfileAccessor:IProfileRepository
    {
        public override bool Insert(Profile entity)
        {
            object rs = IDataProvider.Instance.OExecuteNonQuery("@ProfileID","Profile_Insert",entity.ProfileID,entity.PersonID,entity.FirstName,entity.MiddleName,entity.LastName,entity.DisplayName,entity.DateOfBirth,entity.Email,entity.AddressId,entity.Avatar,entity.AboutMe,entity.IsPrivate);
            return int.Parse(rs.ToString()) > 0;
        }
        public override bool Update(Profile entity)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Profile_Update", entity.PersonID, entity.FirstName, entity.MiddleName, entity.LastName, entity.DisplayName, entity.DateOfBirth, entity.Email, entity.AddressId, entity.Avatar, entity.AboutMe, entity.IsPrivate);
            return rs > 0;
        }
        public override bool Delete(int id)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Profile_Delete", id) > 0;
        }
        public override bool Delete(Profile entity)
        {
            return Delete(entity.ProfileID);
        }
        public override List<Profile> FindAll()
        {
            return CBO.FillCollection<Profile>(IDataProvider.Instance.ExecuteReader("Profile_FindAll"));
        }
        public override Profile FindById(int id)
        {
            Profile p = CBO.FillObject<Profile>(IDataProvider.Instance.ExecuteReader("Profile_FindById", id));          
            return p;
        }
        public override Profile FindByPersonId(int personId)
        {
            return CBO.FillObject<Profile>(IDataProvider.Instance.ExecuteReader("Profile_FindByPersonId", personId));
        }
        public override Profile Single(int id)
        {
            return FindById(id);
        }
        public override Profile FindByName(string name)
        {
            throw new NotImplementedException();
        }
        public override List<Profile> PagingSql(int pageSize, int pageNum, out int totalRowCount)
        {
            IDataReader reader = null;
            reader = IDataProvider.Instance.ExecuteReader("Profile_Paging", pageSize, pageNum);
            reader.Read();
            totalRowCount = reader.GetInt32(0);
            reader.NextResult();
            return CBO.FillCollection<Profile>(reader);
        }
        public override List<Profile> PagingLinq(int pageSize, int pageNum, out int totalRowCount)
        {
             var query = from c in FindAll()
                    select c;
                    totalRowCount = FindAll().Count;
            var list = query.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
            return list;
        }
        public override bool SetActive(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Profile_SetActive", id);
            return rs > 0;
        }
        public override bool SetSortIndex(int id, int isup)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Profile_SetSortIndex", id, isup) > 0;
        }
        public override bool RemoveSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Profile_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RemoveAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Profile_RemoveAll");
            return rs > 0;
        }
        public override bool RestoreSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Profile_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RestoreAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Profile_RestoreAll");
            return rs > 0;
        }
    }
}